#include "app_work.h"
#include "app_flash.h"
#include "bsp_light_sw.h"
#include "bsp_motor.h"

WORK_ST work_info;

static uint8_t run_step = 0;
static uint8_t y_run_time = Y_AXIS_MOVE_TIME;
static uint8_t EndPositFlage = 0;

uint8_t app_work_posit_rst(void);
void app_work_cleaning_run(void);

WORK_ST *app_cfg_work_info(void) { return (&work_info); }

void app_work_init(void) {
  memset(&work_info, 0, sizeof(WORK_ST));
  work_info.workStep = WORK_STOP;
}

void app_work_loop(void) {
  switch (work_info.workStep) {
    case WORK_START:
      if (app_work_posit_rst() == 0x03) {
        work_info.workStep = WORK_RUN;
      }
      break;
    case WORK_RUN:
      app_work_cleaning_run();
      bsp_fan_set(1);
      break;
    case WORK_STOP:
      run_step = 0;
      y_run_time = app_user_flash_get()->dev_Y_step_Time;//Y_AXIS_MOVE_TIME;
      EndPositFlage = 0;
      if (app_work_posit_rst() == 0x03) {
        work_info.workStep = WORK_IDLE;
      }
      bsp_mot_set(BRUSH_MOTOR_GN, FORWARD_TURN, MOTOR_DIS);
      bsp_fan_set(0);
      break;
    case WORK_IDLE:
      bsp_mot_set(BRUSH_MOTOR_GN, FORWARD_TURN, MOTOR_DIS);
      bsp_mot_set(X_MOTOR_GN, FORWARD_TURN, MOTOR_DIS);
      bsp_mot_set(Y_MOTOR_GN, FORWARD_TURN, MOTOR_DIS);
      bsp_fan_set(0);
      break;

    default:
      break;
  }
}

//位置复位
uint8_t app_work_posit_rst(void) {
  uint8_t sta = 0;
  if ((bsp_light_sw_get(X_LIGHT_SW_GN) == X_START_POINT)) {
    bsp_mot_set(X_MOTOR_GN, FORWARD_TURN, MOTOR_DIS);
    sta |= 0x01;
  } else {
    bsp_mot_set(X_MOTOR_GN, REVERSE_TURN, MOTOR_EN);
    sta &= (~0x01);
  }
  if (bsp_light_sw_get(Y_LIGHT_SW_GN) == Y_START_POINT) {
    bsp_mot_set(Y_MOTOR_GN, FORWARD_TURN, MOTOR_DIS);
    sta |= 0x02;
  } else {
    bsp_mot_set(Y_MOTOR_GN, FORWARD_TURN, MOTOR_EN);
    sta &= (~0x02);
  }
  return sta;
}

//开始工作
void app_work_cleaning_run(void) {
  switch (run_step) {
    case 0:
      if (bsp_light_sw_get(X_LIGHT_SW_GN) == X_START_POINT) {
        bsp_mot_set(X_MOTOR_GN, FORWARD_TURN, MOTOR_EN);
        bsp_mot_set(Y_MOTOR_GN, FORWARD_TURN, MOTOR_DIS);
        bsp_mot_set(BRUSH_MOTOR_GN, FORWARD_TURN, MOTOR_EN);
        run_step++;
      }
      break;

    case 1:
      if (bsp_light_sw_get(X_LIGHT_SW_GN) == X_TAIL_POINT) {
        bsp_mot_set(X_MOTOR_GN, REVERSE_TURN, MOTOR_EN);
        run_step++;
      }
      break;
    case 2:
      if (bsp_light_sw_get(X_LIGHT_SW_GN) == X_START_POINT) {
        if (EndPositFlage) {
          work_info.workStep = WORK_STOP;
          EndPositFlage = 0;
          y_run_time = app_user_flash_get()->dev_Y_step_Time;//Y_AXIS_MOVE_TIME;
        } else {
          bsp_mot_set(Y_MOTOR_GN, REVERSE_TURN, MOTOR_EN);
          bsp_mot_set(X_MOTOR_GN, FORWARD_TURN, MOTOR_DIS);
          if (y_run_time) {
            y_run_time--;
          } else {
            y_run_time = app_user_flash_get()->dev_Y_step_Time;//Y_AXIS_MOVE_TIME;
            run_step = 0;
          }
          if (bsp_light_sw_get(Y_LIGHT_SW_GN) == Y_TAIL_POINT) {
            EndPositFlage = 1;
            run_step = 0;
          }
        }
      }
      break;

    default:
      break;
  }
}